package com.xique.api.service.biz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.api.api.IParkApi;
import com.xique.api.bean.domain.ParkingOrder;
import com.xique.api.bean.request.ParkApiCarInAndOutRecordListRequest;
import com.xique.api.bean.request.ParkingOrderInfoRequest;
import com.xique.api.bean.request.ReceiveCouponRequest;
import com.xique.api.bean.search.ParkingOrderSearch;
import com.xique.api.bean.validation.CouponValidation;
import com.xique.api.bean.vo.ParkApiCarInAndOutRecordVO;
import com.xique.api.bean.vo.ParkingOrderVO;
import com.xique.api.service.biz.service.IParkApiBizService;
import com.xique.api.service.service.IParkingOrderService;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.api.RemoteCouponService;
import com.xique.park.api.RemoteParkingOrderService;
import com.xique.park.api.dto.ParkingOrderDTO;
import com.xique.park.api.request.RemoteReceiveCouponRequest;
import com.xique.system.api.RemoteSystemProjectService;
import com.xique.system.api.dto.SystemProjectDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shd
 * @create 2021/11/16 14:56
 */
@Service
public class ParkApiBizServiceImpl implements IParkApiBizService {

    @Autowired
    private IParkApi parkApi;

    @Autowired
    private IParkingOrderService parkingOrderService;

    @Autowired
    private RemoteParkingOrderService remoteParkingOrderService;

    @Autowired
    private RemoteCouponService remoteCouponService;

    @Autowired
    private RemoteSystemProjectService remoteSystemProjectService;

    @Autowired
    private CouponValidation couponValidation;

    @Override
    public AjaxResult getParkingSpaceInfo() {
        JSONObject object = parkApi.space(SecurityUtils.getParkId());
        if (0 != Integer.parseInt(object.get("code").toString())) {
            return AjaxResult.error();
        }
        return new AjaxResult(JSONObject.parseObject(JSONObject.toJSONString(object), AjaxResult.class));
    }

    @Override
    public AjaxResult getCarInAndOutRecordList(ParkApiCarInAndOutRecordListRequest request) {

        ParkingOrderSearch search = new ParkingOrderSearch();
        search.setParkId(SecurityUtils.getParkId());
        search.setPlate(request.getCarPlate());
        search.setStartTime(request.getStartTime());
        search.setEndTime(request.getEndTime());
        search.setPageNum(request.getPageNum());
        search.setPageSize(request.getPageSize());
        IPage<ParkingOrder> page = parkingOrderService.selectParkingOrderPage(search);

        List<ParkingOrder> list = page.getRecords();
        List<ParkApiCarInAndOutRecordVO> vos = OrikaUtil.converts(list, ParkApiCarInAndOutRecordVO.class);
        vos.forEach(item -> {
            if (null != item.getInTime() && null != item.getParkingTime()) {
                item.setOutTime(DateUtils.addSeconds(item.getInTime(), item.getParkingTime().intValue()));
            }
        });

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", vos);
        map.put("total", page.getTotal());

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getParkingOrderInfo(ParkingOrderInfoRequest request) {
        R<ParkingOrderDTO> result = remoteParkingOrderService.getParkingOrder(request.getPlate());
        if (R.FAIL == result.getCode()) {
            return AjaxResult.error(result.getMsg());
        }
        ParkingOrderVO vo = OrikaUtil.convert(result.getData(), ParkingOrderVO.class);
        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult receiveCoupon(ReceiveCouponRequest request) {
        Long parkId = SecurityUtils.getParkId();
        if (StringUtils.isNull(parkId)) {
            return AjaxResult.error("未找到对应的车场id");
        }
        AjaxResult ajaxResult = couponValidation.checkReceiveCouponRequest(request);
        if (AjaxResult.isError(ajaxResult)) {
            return ajaxResult;
        }
        R<SystemProjectDTO> result = remoteSystemProjectService.getProjectInfo(parkId);
        if (R.FAIL == result.getCode()) {
            return AjaxResult.error("未找到对应的车场信息");
        }
        RemoteReceiveCouponRequest remoteReceiveCouponRequest = OrikaUtil.convert(request, RemoteReceiveCouponRequest.class);
        remoteReceiveCouponRequest.setParkId(parkId);
        R<Boolean> r = remoteCouponService.receiveCoupon(remoteReceiveCouponRequest);
        if (R.FAIL == r.getCode()) {
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success();
    }
}
